package threads;

import gui.VideoPane;
import java.awt.Image;
import robot.DBComm;

/**
 * This thread will retrieve the current video frame from the database every 100
 * milliseconds and display it in the video panel.
 *
 * @author Gabriel Loewen
 */
public class VideoThread implements Runnable {

    public static boolean run = false;
    Image frame = null;
    private VideoPane pane;

    /**
     * Constructor.  Takes a VideoPane as input.  This is the special JPanel
     * that will display the video frames.
     *
     * @param p     The VideoPane JPanel that will display the current frame
     */
    public VideoThread(VideoPane p) {
        this.pane = p;
    }

    /**
     * Checks to see if there is an active database connection and then
     * proceeds to retrieve the current frame of video from the database
     * and updating the VideoPane.  This is done every 100ms.
     */
    public void run() {
        run = true;
        while (run) {
            while (DBComm.isConnected()) {
                try {
                    frame = DBComm.getVideoFrame();
                    if (frame != null) {
                        pane.updateImage(frame);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                continue;
            }
        }
    }
}
